package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.SetMeal;
import com.itheima.service.SetMealService;
import com.itheima.utils.QiNiuUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.io.IOException;
import java.util.HashMap;
import java.util.UUID;

import static com.itheima.constant.MessageConstant.PIC_UPLOAD_FAIL;
import static com.itheima.constant.MessageConstant.PIC_UPLOAD_SUCCESS;

@RestController
@RequestMapping("setmeal")
public class SetMealController {
    @Reference(version = "1.0")
    SetMealService setMealService;
    @Autowired
    JedisPool jedisPool;

    @PostMapping("findPage")
    public Result findPage(@RequestBody QueryPageBean queryPageBean) {
        return setMealService.findPage(queryPageBean);
    }

    @PostMapping("save")
    public Result save(@RequestBody SetMeal setMeal){
        Result save = setMealService.save(setMeal);
        if (save.isFlag()){
            try (Jedis jedis = jedisPool.getResource()) {
                jedis.sadd("SETMEAL_SAVE_ALL", setMeal.getImg());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return save;
    }

    @PostMapping("update")
    public Result update (@RequestBody SetMeal setMeal){
        Result update = setMealService.update(setMeal);
        if (update.isFlag()){
            try (Jedis jedis = jedisPool.getResource()) {
                jedis.sadd("SETMEAL_SAVE_ALL", setMeal.getImg());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return update;
    }

    @GetMapping("findById")
    public Result findById(Integer setMealId) {
        return setMealService.findById(setMealId);
    }

    @GetMapping("/delete")
    public Result delete(Integer setMealId){
        return setMealService.delete(setMealId);
    }

    @PostMapping("upload")
    public Result upload(@RequestParam("imgFile") MultipartFile file) {
        String fileName = UUID.randomUUID().toString();
        String originalFilename = file.getOriginalFilename();
        String suffix = StringUtils.substringAfterLast(originalFilename, ".");
        fileName = "setMeal/" + fileName + "." + suffix;
        try {
            boolean upload = QiNiuUtil.upload(file.getBytes(), fileName);
            if (upload) {
                HashMap<String, String> map = new HashMap<>();
                map.put("filename", fileName);
                map.put("imageURL",QiNiuUtil.prefixUrl+fileName);
                try (Jedis jedis = jedisPool.getResource()) {
                    jedis.sadd("SETMEAL_ALL", fileName);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return Result.success(PIC_UPLOAD_SUCCESS,map);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return Result.fail(500, PIC_UPLOAD_FAIL);
    }
}
